package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.Service.CheckItemService;
import com.itheima.comstant.MessageConstant;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceIpml implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;


    //添加检查项
    @Override
    public Result addCheckItem(CheckItem checkItem) {
        checkItemMapper.addCheckItem(checkItem);
        Result result = new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);


        return result;
    }


    //分页查询检测项的内容
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        List<CheckItem> page = checkItemMapper.findPage(queryPageBean);
        PageInfo<CheckItem> checkItemPageInfo = new PageInfo<>(page);
        long total = checkItemPageInfo.getTotal();
        return new PageResult(total,page);
    }




    //按id删除检测项
    @Override
    public void deleteById(int row) {
        //判断是否已经加入了检测组中
       int i= checkItemMapper.findById(row);
       if(i>0){
           throw new RuntimeException("当前检查项被引用，不能删除");
       }else{

           checkItemMapper.deleteById(row);
       }


    }


    //通过id查找到当前的数据进行回显
    @Override
    public CheckItem findByIdToEdit(int row) {

        CheckItem checkItem=checkItemMapper.findByIdToEdit(row);

        return checkItem;
    }


    //添加编辑修改
    @Override
    public void editCheckItem(CheckItem checkItem) {

        checkItemMapper.editCheckItem(checkItem);

    }


    //查询所有的检测项
    @Override
    public List<CheckItem> findAllCheckItem() {
        List<CheckItem> list=checkItemMapper.findAllCheckItem();
        return list;
    }


}
